import {request} from '../../network/index'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {	
		playlist:'',
		songs:[],
		start:10,
		id:'',
		long:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad:async function (options) {
		this.data.id = options.id
		let recommendData = await request('/playlist/detail',{id:options.id})
		let arrayId = recommendData.data.playlist.trackIds.map(item=> item.id)
		this.data.long = arrayId.length
		let musciData = await request(`/song/detail?ids=${arrayId.slice(0,10).join()}`)
    let music = await request(`/song/url?id=${arrayId.join()}`)
		console.log(music);
		this.setData({
			playlist:recommendData.data.playlist,
			songs:musciData.data.songs
		})
	},
	toSongSDetail(event){
		let id = event.currentTarget.dataset.id
		let index = event.currentTarget.dataset.index
		wx.navigateTo({
			url:`/pages/songDetail/songDetail?id=${id}`,
			events:{
				change:(params)=>{
					index += parseInt(params.change);
					if(index<0){
						index = this.data.songs.length-1
					}else if(index>this.data.songs.length-1){
						index = 0
					}
					wx.setStorageSync('musicData',this.data.songs[index])
				}
			},
			success(res){
				res.eventChannel.emit('musciIndex',{index,id})
			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom:async function () {
		this.data.start += 10
		if(this.data.start>this.data.long){
			wx.showToast({
				title: '我也是有底线的',
				icon:'error'
			})
		}else{
			let recommendData = await request('/playlist/detail',{id:this.data.id})
			let arrayId = recommendData.data.playlist.trackIds.map(item=> item.id)
			let arr = arrayId.slice(this.data.start-10,this.data.start)
			console.log(arr);
			let musciData = await request(`/song/detail?ids=${arr.join()}`)
			this.data.songs.push(...musciData.data.songs)
			this.setData({
				songs:this.data.songs
			})
		}
			
		
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})